(function() {
	//加载layer控件
	var layer;
	var element;
	layui.use(['layer','element'], function(){
		layer = layui.layer;
		element = layui.element
	});
	
	/*封装AJAX*/
	jQuery.extend({
    	ORequest: function(url,type,data,dataType,token,async,success){
	    	$.ajax({
	            url:url,
	            type:type, //GET POST
	            async:async,//或false,是否异步
	            data:data,
	            timeout:600000,//超时时间
	            dataType:dataType,//返回的数据格式：json/xml/html/script/jsonp/text
	            beforeSend:function(xhr){
	            	xhr.setRequestHeader("Authorization", token)
	            },
	            success:function(data,textStatus,jqXHR){
	            	if(data.result&&"timeout"==data.result){
	            		window.location.reload();
	            	}else{
	            		if(success){
	            			success(data);
	            		}
	            	}
	            },
	            error:function(xhr,textStatus){
	            	layer.msg(xhr.responseJSON.message, {icon:2,time: 1000});
	            },
	            complete:function(){
	            }
	    	});
	    },
	    OGet: function(url,data,success) {//异步的get请求
	    	this.ORequest(url,"GET",data,'json','',false,success);
	    },
	    OSyncGet: function(url,data,success) {//同步的get请求
	    	this.ORequest(url,"GET",data,'json','',false,success);
	    },
	    OGetHtml: function(url,data,success) {//获取html的get异步请求
	    	this.ORequest(url,"GET",data,'html','',true,function(data){
	    		if(data.substring(0,5)=='error'){
	    		}else{
	    			success(data);
	    		}
	    	});
	    },
	    OPost: function(url,data,success) {//异步post请求
	    	this.ORequest(url,"POST",data,'json','',true,success);
	    },
	    OSyncPost: function(url,data,success) {//同步post请求
	    	this.ORequest(url,"POST",data,'json','',false,success);
	    },
	    OCrossPost:function(url,data,token,success){//跨域post请求异步
	    	this.ORequest(url,"POST",data,'json',token,true,success);
	    },
	    OCrossGet:function(url,data,token,success){//跨域get请求异步
	    	this.ORequest(url,"GET",data,'json',token,true,success);
	    },
	    OCrossPost1:function(url,data,token,success){//跨域post请求同步
	    	this.ORequest(url,"POST",data,'json',token,false,success);
	    },
	    OCrossGet1:function(url,data,token,success){//跨域get请求同步
	    	this.ORequest(url,"GET",data,'json',token,false,success);
	    },
	    LoadPage:function(id,name,url,data,success){//加载页面
			this.OGetHtml(url,data,function(data){
				//通过id查询页面是否存在
				var tab = $(".layui-tab-title").find("[lay-id="+id+"]").length;
				if(tab>0){//存在,进行跳转
					element.tabChange('index-tab', id);
				}else{//不存在,添加并跳转
					element.tabAdd('index-tab', {
		  			  title: name,
		  			  content: data, //支持传入html
		  			  id: id  //'选项卡标题的lay-id属性值'
		  			});
					element.tabChange('index-tab', id);
				}
	    		if(success&&typeof success == 'function'){
	    			success();
	    		}
	    		$(window).resize();
	    	});
	    }
	});

	
	
	//弹出层
	window.popup = function(){
		return {
			//弹出页面 url 接口路径,parmData 参数,title 页面显示标题
			dialog : function(url,parmData,title){
				$.OGetHtml(url,parmData,function(data){
					layer.open({
				      title: title//标题
				      ,type:1
				      ,skin: 'layui-layer-molv'//样式
				      ,maxHeight : '600px'
				      ,maxWidth : '1000px'
				      ,offset: '50px'//坐标
				      ,content: data//内容
				    });
				});
			}
		}
		
	}();
})(jQuery);